package filters;

import draw.ExploringMedics;

/**
 * remove single black points
 */
public class NoiseFilter {

    int imageDescription[][];
    int width = 0;
    int height = 0;

    public NoiseFilter() {
    }

    public NoiseFilter(int[][] imageDescription) {
        this.imageDescription = imageDescription;
        width = imageDescription.length;
        height = imageDescription[0].length;
        doFilted();
    }

    private void doFilted() {
        for (int i = 1; i < width - 1; i++) {
            for (int j = 1; j < height - 1; j++) {

                int blackPoints = 0;
                for (int k = i - 1; k <= i + 1; k++) {
                    for (int p = j - 1; p <= j + 1; p++) {
                        if (imageDescription[k][p] == ExploringMedics.BLACK) {
                            blackPoints++;
                        }
                    }
                }
                
                if (blackPoints == 1 && imageDescription[i][j] == ExploringMedics.BLACK) {
                    imageDescription[i][j] = ExploringMedics.WHITE;
                }
                if (blackPoints == 8 && imageDescription[i][j] == ExploringMedics.WHITE) {
                    imageDescription[i][j] = ExploringMedics.BLACK;
                }
            }


        }
    }

    public void setImageDescription(int[][] imageDescription) {
        this.imageDescription = imageDescription;
        doFilted();
    }

    public int[][] getImageDescription() {
        return imageDescription;
    }
}
